# Values set at CMake configure time
set(CBDIR "@CMAKE_CURRENT_BINARY_DIR@")
set(CSDIR "@CMAKE_CURRENT_SOURCE_DIR@")
set(BIN_DIR "@BIN_DIR@")
set(LOGFILE "${CBDIR}/regress-nurbs_@NHIT_NAME@.log")

set(BU_DIR_CACHE ${CBDIR}/cache)
set(LIBRT_CACHE ${CBDIR}/rtcache)
set(ENV{BU_DIR_CACHE} ${BU_DIR_CACHE})
set(ENV{LIBRT_CACHE} ${LIBRT_CACHE})
file(REMOVE_RECURSE "${BU_DIR_CACHE}")
file(REMOVE_RECURSE "${LIBRT_CACHE}")
file(MAKE_DIRECTORY "${BU_DIR_CACHE}")
file(MAKE_DIRECTORY "${LIBRT_CACHE}")

file(WRITE "${LOGFILE}" "Starting nurbs test run\n")

# The executable locations aren't know at CMake configure time, so one of them
# is passed in via the EXEC variable at runtime.  De-quote it and assign it to
# the appropriate variable.
string(REPLACE "\\" "" NHIT "${EXEC}")
if(NOT EXISTS "${NHIT}")
  file(WRITE "${LOGFILE}" "analyze_nhit not found at location \"${NHIT}\" - aborting\n")
  file(READ "${LOGFILE}" LOG)
  message(FATAL_ERROR "Unable to find analyze_nhit, aborting.\nSee ${LOGFILE} for more details.\n${LOG}")
endif(NOT EXISTS "${NHIT}")

# Canonicalize path - if we have an absolute path use it, but if the .g file is
# a build output we need to find the local configuration's version of the .g
# file.  The caller needs to define a relative path to the root of the build.
get_filename_component(BDIR "${NHIT}" DIRECTORY)
get_filename_component(GFILE_FP "@NHIT_GFILE@" ABSOLUTE)
if("${GFILE_FP}" STREQUAL "@NHIT_GFILE@")
  set(NHIT_G "@NHIT_GFILE@")
else("${GFILE_FP}" STREQUAL "@NHIT_GFILE@")
  string(REGEX REPLACE "${BIN_DIR}$" "" RDIR "${BDIR}")
  set(NHIT_G "${RDIR}/@NHIT_GFILE@")
endif("${GFILE_FP}" STREQUAL "@NHIT_GFILE@")

execute_process(
  COMMAND "${NHIT}" @NHIT_OPTS@ "${NHIT_G}" @NHIT_ARGS@
  RESULT_VARIABLE nurbs_result
  OUTPUT_VARIABLE nurbs_log
  ERROR_VARIABLE nurbs_log
  WORKING_DIRECTORY ${CBDIR}
)

if(nurbs_result)
  file(READ "${LOGFILE}" LOG)
  message(
    FATAL_ERROR
    "[nurbs] Failure, unexpected result running ${NHIT} @NHIT_OPTS@ @NHIT_GFILE@ @NHIT_ARGS@\n ${nurbs_log}\n${LOG}"
  )
endif(nurbs_result)

# Cleanup
execute_process(
  COMMAND "@CMAKE_COMMAND@" -E rm -rf ${BU_DIR_CACHE}
)
execute_process(
  COMMAND "@CMAKE_COMMAND@" -E rm -rf ${LIBRT_CACHE}
)

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
